package logic

import (
	"context"
	"errors"
	"goods_srv/models/mysqlinit"
	"time"

	"goods_srv/goods"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type InfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InfoLogic {
	return &InfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *InfoLogic) Info(in *goods.InfoRequest) (*goods.InfoResponse, error) {
	// todo: add your logic here and delete this line
	var house mysqlinit.House
	mysqlinit.DB.Debug().Where("id = ?", in.Id).Find(&house)
	if house.ID == 0 {
		logx.Error("房源不存在")
		return nil, errors.New("房源不存在")
	}

	return &goods.InfoResponse{
		HouseTitle: house.HouseTitle,
		HouseRent:  float32(house.HouseRent),
		Address:    house.Address,
		Community:  house.Community,
		FloorCode:  house.FloorCode,
		FloorNum:   int64(house.FloorNum),
		HouseArea:  int64(house.HouseArea),
		Status:     house.Status,
		CreatedAt:  house.CreatedAt.Format(time.DateTime),
	}, nil
}
